﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace FileMonitor.Configuration.Tool.Controls.Prototype
{
    public partial class ProcessEditorBase : UserControl
    {
        protected bool IsCanbeSaved=true;
        public ProcessEditorBase()
        {
            InitializeComponent();
        }

      

        private Mode _mode;

        public Mode Mode
        {
            get { return this._mode; }
            set
            {
                if (value == Mode.Edit)
                {
                    this.txt_ProcessName.ReadOnly = true;
                    this.btnDelete.Visible = true;
                }
                this._mode = value;
            }
        }

        public delegate void SaveHandler(object sender, ProccessArgs args);
        public delegate void DeleteHandler(object sender, ProccessArgs args);

        public event SaveHandler Save;
        public event DeleteHandler Delete;
 
        private void OnSave(object sender, ProccessArgs args)
        {
            if (Save != null)
            {
                if (IsCanbeSaved)
                {
                    Save(this, args);
                }
                else
                {
                    MessageBox.Show("There are errors,can't be save!!");
                }
            }
        }

        private void OnDelete(object sender, ProccessArgs args)
        {
            if (Delete != null)
            {
                Delete(this, args);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string ProcessName = this.txt_ProcessName.Text;
            NameValueCollection normalConfig = GetNormalConfig();
            NameValueCollection extConfig = GetExtConfig();
            OnSave(this, new ProccessArgs(ProcessName, normalConfig, extConfig));
        }

        public void BindData(string ProcessName, NameValueCollection NormalConfig, NameValueCollection ExtendableConfig)
        {
            this.txt_ProcessName.Text = ProcessName;
            foreach (string key in NormalConfig.Keys)
            {
                string id = "txt_" + key;
                if (!this.tabNormal.Controls.ContainsKey(id))
                {
                    id = "box_" + key;
                }
                Control[] crtlArr = this.tabNormal.Controls.Find(id, false);
                if (crtlArr.Length == 1)
                {
                    crtlArr[0].Text = NormalConfig[key];
                }
            }

            foreach (string key in ExtendableConfig.Keys)
            {
                string id = "txt_" + key;
                if (!this.tabExtendabel.Controls.ContainsKey(id))
                {
                    id = "box_" + key;
                }
                Control[] crtlArr = this.tabExtendabel.Controls.Find(id, false);
                if (crtlArr.Length == 1)
                {
                    crtlArr[0].Text = NormalConfig[key];
                }
            }

        }

        private NameValueCollection GetNormalConfig()
        {
            NameValueCollection values = new NameValueCollection();
            foreach (Control crtl in this.tabNormal.Controls)
            {

                if (crtl is TextBox || crtl is ComboBox)
                {
                    string key = crtl.Name.Substring(crtl.Name.IndexOf("_") + 1, crtl.Name.Length - crtl.Name.IndexOf("_") - 1);
                    string value = crtl.Text;
                    values.Add(key, value);
                }
            }
            return values;
        }

        private NameValueCollection GetExtConfig()
        {
            NameValueCollection values = new NameValueCollection();
            foreach (Control crtl in this.tabExtendabel.Controls)
            {

                if (crtl is TextBox || crtl is ComboBox)
                {
                    string key = crtl.Name.Substring(crtl.Name.IndexOf("_") + 1, crtl.Name.Length - crtl.Name.IndexOf("_") - 1);
                    string value = crtl.Text;
                    values.Add(key, value);
                }
            }
            return values;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ProccessArgs args = new ProccessArgs(this.txt_ProcessName.Text, this.GetNormalConfig(), this.GetExtConfig());
            OnDelete(this, args);
        }
    }
}
